﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LogModule.Interface;
using LogModule.Interface.Imp;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Mvvm;
using StaffModule.DbContexts;
using StaffModule.Enums;
using StaffModule.Models;
using StaffModule.Utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace StaffModule.ViewModels {
    public partial class AddStaffViewModel : ObservableObject,IDialogAware {
        #region 字段
        private readonly StaffDbContext _staffDbContext;
        private readonly ILogService _logService;
        #endregion

        public  AddStaffViewModel(StaffDbContext StaffDbContext, ILogService logService) {
            _logService = logService;
            _logService.Info("添加员工弹窗初始化");

            #region 初始化
            _staffDbContext = StaffDbContext;
            StaffDetails = new StaffDetails() { Age=18};
           
            AgeList = new ObservableCollection<int?>();
            for (int i = 18; i <= 60; i++) {
                AgeList.Add(i);
            }
            #endregion

        }

      




        #region 命令

        //对提交的信息进行校验
        [RelayCommand]
        private void SubmitStaff() {
            //必填项是否为空,每一项是否符合规范
            //1. 员工编号是字符串,查看数据库中是否有重复的(应该自己生成,这样不用比对数据库)
            var EmployeeId =RulesClass.GenerateEmployeeId();
            StaffDetails.EmployeeId = EmployeeId;
            //2. 员工姓名大体没有限制
            if(StaffDetails.Name == null || StaffDetails.Name == "") {//判断是否为空
                //通知
                MessageBox.Show("员工姓名不能为空");
                _logService.Warn("添加员工: 员工姓名不能为空");
            }
            //3. 年龄是下拉框,没有限制

            //4. 职位也是下拉框

            //5. 手机号码需要校验
            if (StaffDetails.Phone == null || StaffDetails.Phone == "" || !RulesClass.IsMobile(StaffDetails.Phone)) {//判断是否为空,是否符合规则
                //通知
                 MessageBox.Show("手机号码不能为空且需要合法");
                _logService.Warn("添加员工: 手机号码不能为空且需要合法");
            }
           
            //手机号码校验//判断是否符合规则,用静态方法
            //如果不符合规范/为空等等,需要通知

            //如何符合规范,提交到数据库
            try {
                _staffDbContext.StaffDetails.AddAsync(StaffDetails);
                _staffDbContext.SaveChangesAsync();
                MessageBox.Show("添加成功");
                _logService.Info("添加员工成功");
                RequestClose.Invoke(new DialogResult(ButtonResult.OK));
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
            RequestClose.Invoke(new DialogResult(ButtonResult.No));
        }


        #endregion

        #region 对话框
        public DialogCloseListener RequestClose { get; }

        //是否能够关闭对话框,就是在关闭对话框的时候调用1
        public bool CanCloseDialog() {
            return true;
        }
        //关闭,就是在关闭对话框的时候调用2
        public void OnDialogClosed() {
          
        }
        //打开对话框,就是在打开对话框的时候调用,传递参数
        public void OnDialogOpened(IDialogParameters parameters) {
            Title = parameters.GetValue<string>("Title");
        }

        #endregion

        #region 属性
        [ObservableProperty]
        private string _title  = " ";

        [ObservableProperty]
        private StaffDetails _staffDetails;

        [ObservableProperty]
        private ObservableCollection<int?> _ageList;

        [ObservableProperty]
        private ObservableCollection<string> _positionList = new ObservableCollection<string>() { "Test1", "Test2", "Test3", "Test4", "Test5" };

        #endregion


    }
}
